package com.kekezu.kppw.data;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;

import com.kekezu.kppw.R;
import com.kekezu.kppw.utils.ConfigInc;
import com.kekezu.kppw.utils.HttpUtils;

public class IndexData {
	/**
	 * 首页顶部幻灯片
	 * 
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> getIndexImageList(
			Context context) {
		ArrayList<HashMap<String, Object>> imageList = new ArrayList<HashMap<String, Object>>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/slideInfo";
		HttpUtils httpUtils = HttpUtils.getInstance();

		try {
			String strJson = httpUtils.httpGet(urlString, context);

			JSONObject jsonObject = new JSONObject(strJson);
			String resString = jsonObject.getString("code");

			// 对返回的数据进行判断
			if (resString.equals("1000")) {
				JSONArray jsonArray = jsonObject.getJSONArray("data");

				if (jsonArray.length() <= 0) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("ad_file", R.drawable.user_bg);
					map.put("ad_url", ConfigInc.getWebURL(context));

					imageList.add(map);
				} else {
					for (int i = 0; i < jsonArray.length(); i++) {
						HashMap<String, Object> map = new HashMap<String, Object>();
						JSONObject itemJsonObject = jsonArray.optJSONObject(i);
						map.put("ad_file", itemJsonObject.getString("ad_file"));
						map.put("ad_url", itemJsonObject.getString("ad_url"));

						imageList.add(map);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return imageList;
	}

}
